package cn.xn.hrms.api.controller;

import java.util.List;

import javax.annotation.Resource;

import cn.xn.hrms.api.dto.res.MenuChildDTO;
import cn.xn.hrms.api.dto.res.RoleDTO;
import cn.xn.hrms.domain.service.MenuService;
import cn.xn.hrms.api.dto.req.RoleMenuReqDTO;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;

import cn.xn.hrms.domain.service.RoleService;
import cn.xn.hrms.domain.utils.ResultVo;
import io.swagger.annotations.ApiOperation;

/**
 * @author: ysz
 * @date: 2023/08/06
 **/
@RestController
@RequestMapping("/role")
public class RoleController {


    @Resource
    private RoleService roleService;

    @Resource
    private MenuService menuService;

    /**
     * 新增角色
     *
     * @param roleDTO
     * @return
     */
    @ApiOperation("新增角色")
    @PostMapping("/add")
    @RequiresRoles("admin")
    public ResultVo roleAdd(@RequestBody RoleDTO roleDTO) {
        roleService.add(roleDTO);
        return ResultVo.success();
    }

    /**
     * 删除角色
     *
     * @param
     * @return
     */
    @ApiOperation("删除角色(支持批量)")
    @DeleteMapping("/delete")
    @RequiresRoles("admin")
    public ResultVo roleDel(@RequestBody List<Integer> ids) {
        roleService.delete(ids);
        return ResultVo.success();

    }

    /**
     * 角色列表
     *
     * @return
     */
    @ApiOperation("角色列表")
    @GetMapping("/list")
    public List<RoleDTO> roleList() {
        return roleService.list();
    }

    /**
     * 修改角色
     *
     * @param roleDTO
     * @return
     */
    @ApiOperation("修改角色")
    @PutMapping("/update")
    @RequiresRoles("admin")
    public ResultVo roleUpdate(@RequestBody RoleDTO roleDTO) {
        roleService.update(roleDTO);
        return ResultVo.success();
    }

    /**
     * 批量查询角色列表
     *
     * @param page
     * @param size
     * @param name
     * @param description
     * @return
     */

    @ApiOperation("分页查询角色")
    @GetMapping("/paged-list")
    PageInfo<RoleDTO> rolePageList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                   @RequestParam(value = "size", defaultValue = "10") Integer size,
                                   @RequestParam(required = false) String name,
                                   @RequestParam(required = false) String description){

        return roleService.pageList(page, size, name, description);

    }

    /**
     * 给角色分配菜单
     *
     * @param roleMenuReqDTO
     * @return
     */
    @ApiOperation("给角色分配菜单")
    @PostMapping("/saveMenus")
    @RequiresRoles("admin")
    public ResultVo roleSaveMenus(@RequestBody RoleMenuReqDTO roleMenuReqDTO) {
        roleService.saveRoleMenus(roleMenuReqDTO);
        return ResultVo.success();
    }

    /**
     * 查询角色已分配的菜单
     *
     * @param roleId
     * @return
     */
    @ApiOperation("查询角色菜单列表（包含已分配和未分配）")
    @GetMapping("/{roleId}/menuList")
    public List<MenuChildDTO> getRoleMenuList(@PathVariable Long roleId) {
        return menuService.getMenuList(roleId);
    }



}
